package com.zzyl.controller.customer;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.dto.ReservationDto;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/customer/reservation")
@Api(tags = "客户预约管理")
public class CustomerReservationController extends BaseController {
    @Autowired
    private CustomerReservationService customerReservationService;

    /**
     * 查询取消预约数量
     *
     * @return
     */
    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public ResponseResult<Integer> getCancelledReservationCount() {
        Long userId = UserThreadLocal.getUserId();
        Integer count = customerReservationService.getCancelledReservationCount(userId);
        return success(count);
    }

    /**
     * 查询每个时间段剩余预约次数
     *
     * @param time
     * @return
     */
    @ApiOperation("查询每个时间段剩余预约次数")
    @GetMapping("countByTime")
    public ResponseResult<List<TimeCountVo>> queryCountByTime(@ApiParam("时间戳") Long time) {

        log.info("查询当前时间:{}", new SimpleDateFormat("yyyy-MM-dd ").format(time));
        List<TimeCountVo> vo = customerReservationService.queryCountByTime(time);
        return success(vo);
    }

    /**
     * 新增预约
     *
     * @param dto
     * @return
     */
    @ApiOperation("新增预约")
    @PostMapping
    public ResponseResult addReservation(@RequestBody ReservationDto dto) {

        customerReservationService.addReservation(dto);
        return success();
    }

    /**
     * 分页查询预约
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation("分页查询预约")
    @GetMapping("/page")
    public ResponseResult<PageResponse<ReservationVo>> queryPage(Integer status,
                                                                 @ApiParam(name = "pageNum", value = "当前页码")
                                                                 @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                                 @ApiParam(name = "pageSize", value = "总条数")
                                                                 @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        PageResponse<ReservationVo> vo = customerReservationService.queryPage(status, pageNum, pageSize);
        return success(vo);
    }
    /**
     * 取消预约
     */
    @ApiOperation("取消预约")
    @PutMapping("/{id}/cancel")
    public ResponseResult modifyStatus(@PathVariable Long id){

        customerReservationService.modifyStatus(id);
        return success();
    }
}